package ucap.com.lesanapp.activitys;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import ucap.com.lesanapp.R;
import ucap.com.lesanapp.base.impl.TopNewsBase;
import ucap.com.lesanapp.global.SettingsModelState;
import ucap.com.lesanapp.utils.PrefUtils;
import ucap.com.lesanapp.view.SettingsSelectModelView;

public class SettingsModelActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.rl_text_and_image)
    RelativeLayout rlImageAndText;
    @Bind(R.id.btn_image_and_text)
    RadioButton btnImageAndText;

    @Bind(R.id.rl_text)
    RelativeLayout rlText;
    @Bind(R.id.btn_text)
    RadioButton btnText;

    @Bind(R.id.rl_automatic)
    RelativeLayout rlAuto;
    @Bind(R.id.btn_auto)
    RadioButton btnAuto;


    private int model;
    int saveModel = SettingsModelState.MODEL_TEXT_AND_IMAGE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_model);
        ButterKnife.bind(this);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        intiDate();
        intiListener();
    }

    private void intiListener() {
        rlImageAndText.setOnClickListener(this);
        rlText.setOnClickListener(this);
        rlAuto.setOnClickListener(this);
        btnImageAndText.setOnClickListener(this);
        btnText.setOnClickListener(this);
        btnAuto.setOnClickListener(this);
    }


    private void intiDate() {
        model = PrefUtils.getInt("model", SettingsModelState.MODEL_TEXT_AND_IMAGE, getApplicationContext());
        Log.i("saveModel", model + "----");
        if (model == SettingsModelState.MODEL_TEXT_AND_IMAGE) {
            btnImageAndText.setChecked(true);
            btnText.setChecked(false);
            btnAuto.setChecked(false);
        } else if (model == SettingsModelState.MODEL_TEXT) {
            btnImageAndText.setChecked(false);
            btnText.setChecked(true);
            btnAuto.setChecked(false);
        } else if (model == SettingsModelState.MODEL_AUTOMATIC) {
            btnImageAndText.setChecked(false);
            btnText.setChecked(false);
            btnAuto.setChecked(true);
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        int model = PrefUtils.getInt("model", SettingsModelState.MODEL_TEXT_AND_IMAGE, this);
        switch (model) {
            case SettingsModelState.MODEL_TEXT_AND_IMAGE://图文模式
                Toast.makeText(this, "你当前已设置为\"图文模式\"下拉刷新后立即生效!", Toast.LENGTH_SHORT).show();
                break;
            case SettingsModelState.MODEL_TEXT://图文模式
                Toast.makeText(this, "你当前已设置为\"文本模式\"下拉刷新后立即生效!", Toast.LENGTH_SHORT).show();
                break;
            case SettingsModelState.MODEL_AUTOMATIC://图文模式
                Toast.makeText(this, "你当前已设置为\"智能模式\"下拉刷新后立即生效!", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);//友盟统计

    }

    @Override
    public void onClick(View view) {
        if (view == rlImageAndText) {
            btnImageAndText.setChecked(true);
            btnText.setChecked(false);
            btnAuto.setChecked(false);
            PrefUtils.putInt("model", SettingsModelState.MODEL_TEXT_AND_IMAGE, this);
        } else if (view == rlText) {
            btnImageAndText.setChecked(false);
            btnText.setChecked(true);
            btnAuto.setChecked(false);
            PrefUtils.putInt("model", SettingsModelState.MODEL_TEXT, this);
        } else if (view == rlAuto) {
            btnImageAndText.setChecked(false);
            btnText.setChecked(false);
            btnAuto.setChecked(true);
            PrefUtils.putInt("model", SettingsModelState.MODEL_AUTOMATIC, this);

        }
        //如果直接点击选择按钮
        else if (view == btnImageAndText) {
            //getParent().requestDisallowInterceptTouchEvent(true);
            btnImageAndText.setChecked(true);
            btnText.setChecked(false);
            btnAuto.setChecked(false);
            PrefUtils.putInt("model", SettingsModelState.MODEL_TEXT_AND_IMAGE, this);
        } else if (view == btnText) {
            btnImageAndText.setChecked(false);
            btnText.setChecked(true);
            btnAuto.setChecked(false);
            PrefUtils.putInt("model", SettingsModelState.MODEL_TEXT, this);
        } else if (view == btnAuto) {
            btnImageAndText.setChecked(false);
            btnText.setChecked(false);
            btnAuto.setChecked(true);
            PrefUtils.putInt("model", SettingsModelState.MODEL_AUTOMATIC, this);
        }
    }
}
